<template>
  <div class="zh-organization organizational-s">
    <nav-header></nav-header>
    <div class="zh-table">
      <div class="zh-table-open clear">
        <div class="zh-open-right fr">
          <el-button type="primary" @click="getOrgData">刷新</el-button>
        </div>
      </div>
      <div class="zh-tree-contents " v-loading="loading.status" :element-loading-text="loading.txt">
        <div class="frame-manage">
          <ul class="frame-list">
            <li>
              <div class="ellips">序号</div>
            </li>
            <li>
              <div class="ellips">名称</div>
            </li>
            <li>
              <div class="ellips">超级管理员</div>
            </li>
            <li>
              <div class="ellips">状态</div>
            </li>
            <li>
              <div class="ellips">操作</div>
            </li>
          </ul>
          <el-tree :data="orgData" :props="defaultProps" node-key="id" default-expand-all>
            <div class="custom-tree-node" slot-scope="{ node, data }">
              <ul class="frame-list">
                <li>
                  <div class="ellips">{{ orgData[0].inx++ }}</div>
                </li>
                <li>
                  <div class="ellips">
                    <span v-show="node.level >= 2" class="gap"></span>
                    <span v-show="node.level >= 3" class="gap"></span>
                    <span v-show="node.level >= 4" class="gap"></span>
                    <span v-show="node.level >= 5" class="gap"></span>
                    <span v-show="node.level >= 6" class="gap"></span>
                    <span v-show="node.level >= 7" class="gap"></span>
                    <span v-show="node.level >= 8" class="gap"></span>
                    <span v-show="node.level >= 9" class="gap"></span>
                    <img v-if="data.server_type === 2" src="/static/image/user/edu.png">
                    <img v-if="data.server_type === 1" src="/static/image/user/sch.png">
                    <img v-if="data.level === 2" src="/static/image/user/tree2.png">
                    <img v-if="data.level === 3" src="/static/image/user/tree3.png">
                    <img v-if="data.level === 4" src="/static/image/user/tree4.png">
                    <span v-html="data.name"></span>
                  </div>
                </li>
                <li>
                  <div class="ellips" v-if="node.level === 1"><span>-</span></div>
                  <div class="ellips" v-else-if="data.admin_id"><span>{{ data.admin_name }}</span></div>
                  <div class="ellips" v-else><span>-</span></div>
                </li>
                <li>
                  <div class="ellips" v-if="node.level === 1"><span class="link"> - </span></div>
                  <div class="ellips" v-else><span class="link">{{ data.status.toString() | formatStatus }}</span></div>
                </li>
                <li style="text-align:left;">
                  <div style="width:100%;height:100%;padding-left:60px;box-sizing:border-box;">
                    <!-- 编辑教育局 -->
                    <el-button type="text" size="mini" v-if="data.server_type === 2"
                      @click.stop="addEducationTrigger(data, '编辑')">编辑</el-button>
                    <!-- 添加教育局------------------------------- server_type 2 为教育局 可添加教育局与学校 node.level 最高九级--------------------------- -->
                    <el-button type="text" size="mini" v-if="data.server_type === 2 && node.level !== 9"
                      @click.stop="addEducationTrigger(data, '添加')">添加教育局</el-button>

                    <!-- 添加学校 -->
                    <el-button type="text" size="mini" v-if="data.server_type === 2"
                      @click.stop="addSchoolTrigger(data, '添加')">添加学校</el-button>
                    <!-- 编辑学校 -->
                    <el-button type="text" size="mini" v-else-if="data.level == 1 && data.server_type == 1"
                      @click.stop="addSchoolTrigger(data, '编辑')">编辑</el-button>

                    <!-- 添加学段/ 院系/ 类别---------------------------------- -->
                    <el-button type="text" size="mini" v-if="data.server_type === 1"
                      @click.stop="addFacultyTrigger(data, '添加')">{{ data.cate_type.toString() | forCatetype
                      }}</el-button>
                    <!-- 编辑学段/ 院系/ 类别-->
                    <el-button type="text" size="mini" v-if="data.level === 2"
                      @click.stop="addFacultyTrigger(data, '编辑')">编辑</el-button>

                    <!-- 添加年级/专业----------------------------------- -->
                    <el-button type="text" size="mini" v-if="data.level === 2"
                      @click.stop="addGradeFlagTrigger(data, '添加')">添加{{ data.cate_type === 1 ? '年级' :
            '专业' }}</el-button>
                    <!-- 编辑年级/专业----------------------------------- -->
                    <el-button type="text" size="mini" v-if="data.level === 3"
                      @click.stop="addGradeFlagTrigger(data, '编辑')">编辑</el-button>

                    <!-- 添加班级---------------------------------------------------- -->
                    <el-button type="text" size="mini" v-if="data.level === 3"
                      @click.stop="addClassFlagTrigger(data, '添加')">添加班级</el-button>
                    <!-- 编辑班级---------------------------------------------------- -->
                    <el-button type="text" size="mini" v-if="data.level === 4"
                      @click.stop="addClassFlagTrigger(data, '编辑')">编辑</el-button>

                    <!-- 禁用/启用 教育局/学校/ 组织架构------------------------------------------- -->
                    <el-button type="text" v-if="node.level !== 1 && data.status == 1" size="mini" key="data"
                      class="delete-button" @click.stop="stopEducationTrigger(data, '禁用')">禁用</el-button>
                    <el-button type="text" v-if="node.level !== 1 && data.status == 2" size="mini"
                      @click.stop="stopEducationTrigger(data, '启用')">启用</el-button>
                    <!-- 删除教育局/学校/ 组织架构------------------------------------------------- -->
                    <el-button type="text" v-if="node.level !== 1" size="mini" class="delete-button"
                      @click.stop="stopEducationTrigger(data, '删除')">删除</el-button>
                  </div>
                </li>
              </ul>
            </div>
          </el-tree>
        </div>
      </div>
    </div>
    <!--弹窗 添加/编辑 教育局 --------------------------------------------------------------------- -->
    <el-dialog :title="edutitle + '教育局'" :visible.sync="addEducationFlag" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="handleClose" width="40%">
      <el-form ref="educationForm" :model="educationForm" :rules="rules" label-width="120px" @submit.native.prevent>
        <el-form-item label="教育局名称" prop="eduname">
          <el-input type="text" placeholder="请输入教育局名称" v-model.trim="educationForm.eduname" maxlength="20"> </el-input>
        </el-form-item>
        <el-form-item label="管理员账号" prop="admin_number">
          <el-input type="text" placeholder="创建管理员账号" v-model.trim="educationForm.admin_number" maxlength="20">
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="admin_password">
          <el-input type="password" placeholder="不填时默认为666666" v-model.trim="educationForm.admin_password"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addEducationTips()">取 消</el-button>
        <el-button type="primary" @click="addEducationTips(1)">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加/编辑 学校begin -->
    <el-dialog :title="schtitle + '学校'" :visible.sync="addSchoolFlag" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="handleClose" width="40%">
      <el-form ref="schoolForm" :model="schoolForm" :rules="rules" label-width="120px" @submit.native.prevent>
        <el-form-item label="学校名称" prop="schname">
          <el-input type="text" placeholder="请输入学校名称" v-model.trim="schoolForm.schname" maxlength="10"> </el-input>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="schoolForm.cate_type">
            <el-radio v-if="schtitle === '添加'" :label="1">普通教育</el-radio>
            <el-radio v-else :disabled="(schoolChild.length === 0) ? false : true" :label="1">普通教育</el-radio>
            <el-radio v-if="schtitle === '添加'" :label="2">高等教育</el-radio>
            <el-radio v-else :disabled="(schoolChild.length === 0) ? false : true" :label="2">高等教育</el-radio>
            <el-radio v-if="schtitle === '添加'" :label="3">成人教育</el-radio>
            <el-radio v-else :disabled="(schoolChild.length === 0) ? false : true" :label="3">成人教育</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管理员账号" prop="admin_number">
          <el-input type="text" placeholder="创建管理员账号" v-model.trim="schoolForm.admin_number" maxlength="20"> </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="admin_password">
          <el-input type="password" placeholder="不填时默认为666666" v-model.trim="schoolForm.admin_password"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addSchoolTips()">取 消</el-button>
        <el-button type="primary" @click="addSchoolTips(1)">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加/编辑 学校end -->

    <!--弹窗 禁用/启用/删除 教育局/学校/组织架构--------------------------------------------------- -->
    <el-dialog :title="stopTitle" :visible.sync="delEducationFlag" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="handleClose" width="40%">
      <span style="padding-left: 10px;display: block;margin-bottom: 30px;">{{ (stopTitle === '启用') ? null :
            '该操作将造成严重不可逆影响，' }}是否确认{{ stopTitle }}{{ stopName }}？</span>
      <el-form ref="delEducationForm" :model="delEducationForm" :rules="rules" label-width="120px"
        @submit.native.prevent>
        <el-form-item label="超级管理员密码" prop="admin_password">
          <el-input type="password" placeholder="" v-model.trim="delEducationForm.admin_password"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="delEducationTips()">取 消</el-button>
        <el-button type="primary" @click="delEducationTips(1)">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 弹窗 禁用/启用/删除教育局/学校--------------------------------------------------- -->

    <!-- 添加/编辑 学段 ------------------------------------------------ 弹窗 -->
    <el-dialog :title="FacultyTitle" :visible.sync="addFacultyFlag" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="handleClose" width="40%">
      <el-form ref="addFacultyForm" :model="addFacultyForm" :rules="rules" label-width="120px" @submit.native.prevent>
        <el-form-item label="类型" prop="type">
          <el-radio-group v-model="addFacultyForm.cate_type">
            <el-radio v-if="FacultyTitle === '添加学段' || FacultyTitle === '编辑学段'" :label="1">普通教育</el-radio>
            <el-radio v-else-if="FacultyTitle === '添加院系' || FacultyTitle === '编辑院系'" :label="2">高等教育</el-radio>
            <el-radio v-else :label="3">成人教育</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="FacultyTitle === '添加学段' || FacultyTitle === '编辑学段'" label="选择学段" prop="staryname">
          <el-select v-model="addFacultyForm.staryname" placeholder="请选择添加的学段">
            <el-option label="小学" value="小学">小学</el-option>
            <el-option label="初中" value="初中">初中</el-option>
            <el-option label="高中" value="高中">高中</el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else-if="FacultyTitle === '添加院系' || FacultyTitle === '编辑院系'" label="院系" prop="facultyname">
          <el-input type="text" placeholder="请输入院系名称" v-model.trim="addFacultyForm.facultyname" maxlength="20">
          </el-input>
        </el-form-item>
        <el-form-item v-else label="类别" prop="kindname">
          <el-input type="text" placeholder="请输入类别，如建筑工程类" v-model.trim="addFacultyForm.kindname" maxlength="20">
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addFacultyTips()">取 消</el-button>
        <el-button type="primary" @click="addFacultyTips(1)">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加/编辑 学段 ------------------------------------------------ 弹窗 -->

    <!-- 添加/编辑 年级/专业 ------------------------------------------------ 弹窗 -->
    <el-dialog :title="gradeTitle" :visible.sync="addGradeFlag" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="handleClose" width="40%">
      <el-form ref="addGradeForm" :model="addGradeForm" :rules="rules" label-width="120px" @submit.native.prevent>
        <el-form-item v-if="gradeTitle === '添加年级' || gradeTitle === '编辑年级'" label="输入年级：" prop="gradename">
          <el-input type="text" placeholder="请输入年级名称" v-model.trim="addGradeForm.gradename" maxlength="20"> </el-input>
        </el-form-item>
        <el-form-item v-else label="专业名称：" prop="majorname">
          <el-input type="text" placeholder="请输入专业名称" v-model.trim="addGradeForm.majorname" maxlength="20"> </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addGradeTips()">取 消</el-button>
        <el-button type="primary" @click="addGradeTips(1)">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加/编辑 年级/专业 ------------------------------------------------ 弹窗 -->

    <!-- 添加/编辑 班级 ------------------------------------------------ 弹窗 -->
    <el-dialog :title="classTitle + '班级'" :visible.sync="addClassFlag" :close-on-click-modal="false"
      :close-on-press-escape="false" @close="handleClose" width="40%">
      <el-form ref="addClassForm" :model="addClassForm" :rules="rules" label-width="120px" @submit.native.prevent>
        <el-form-item label="班级名称：" prop="classname">
          <el-input type="text" placeholder="请输入班级名称" v-model.trim="addClassForm.classname" maxlength="10"> </el-input>
          <!-- <el-select v-model="addClassForm.classname" filterable placeholder="请选择添加的班级">
                        <el-option
                        v-for="(item, index) in addClassList"
                        :key="index"
                        :label="item"
                        :value="item"
                        ></el-option>
                    </el-select> -->
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addClassTips()">取 消</el-button>
        <el-button type="primary" @click="addClassTips(1)">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加/编辑 班级 ------------------------------------------------ 弹窗 -->
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: {
        status: false,
        txt: "获取数据..."
      },
      defaultProps: {
        label: 'name',
      },
      orgData: [], //组织架构数据
      edutitle: '',
      addEducationFlag: false,
      educationForm: {
        eduname: '',/*记录添加教育局名称 */
        admin_number: '',/*记录管理员账号 */
        admin_password: '',/*记录管理员密码 */
        server_type: '',
        pid: '',
        id: '',
      },
      schtitle: '',
      addSchoolFlag: false,
      schoolForm: {
        schname: '',/*记录添加学校名称 */
        cate_type: 1, /**记录类型，1为普通教育，2为高等教育，3为成人教育 */
        server_type: '',
        pid: '',
        admin_number: '',/*记录管理员账号 */
        admin_password: '',/*记录管理员密码 */
        id: '',
      },
      schoolChild: '',
      stopTitle: '',
      stopName: '',
      levelFlag: '',
      stopObj: {},
      delEducationFlag: false,
      delEducationForm: {
        admin_password: ''
      },
      cat_Faculty: '',
      FacultyTitle: '',
      FacultyName: '',
      addFacultyFlag: false,
      addFacultyForm: {
        cate_type: '',
        staryname: '', // 学段名称
        facultyname: '', // 院系名称
        kindname: '', // 类别名称
        status: '',
        app_code: '',
        level: '',
        pid: '',
      },
      addGradeFlag: false,
      gradeTitle: '',
      gradeEven: '',
      addGradeForm: {
        cate_type: '',
        gradename: '', // 年级名称
        majorname: '', // 专业名称
        pid: '',
        level: '',
        status: '',
        app_code: '',
      },
      addClassFlag: false,
      classTitle: '',
      // addClassList: [] /*存放未创建班级列表 */,
      addClassForm: {
        classname: '', // 班级名称
        pid: '',
        level: '',
        status: '',
        app_code: '',
      },
      rules: { // 正则校验
        eduname: [ // 教育局
          { required: true, validator: this.checkValue, message: '请填写教育局名称' },
          { pattern: this.rules.chinese.reg, message: this.rules.chinese.msg, trigger: 'blur' }
        ],
        schname: [ // 学校
          { required: true, validator: this.checkValue, message: '请输入学校名称' },
          { pattern: this.rules.chinese.reg, message: this.rules.chinese.msg, trigger: 'blur' }
        ],
        staryname: [
          { message: '请选择学段', validator: this.checkValue },
        ],
        facultyname: [ // 高等教育院系/成人教育类别
          { message: '请输入院系名称', validator: this.checkValue },
          { pattern: this.rules.chinese.reg, message: this.rules.chinese.msg, trigger: 'blur' }
        ],
        kindname: [
          { message: '请输入类别名称', validator: this.checkValue },
          { pattern: this.rules.chinese.reg, message: this.rules.chinese.msg, trigger: 'blur' }
        ],
        gradename: [
          { message: '请输入年级名称', validator: this.checkValue },
          { pattern: this.rules.chinese.reg, message: this.rules.chinese.msg, trigger: 'blur' }
        ],
        majorname: [
          { message: '请输入专业名称', validator: this.checkValue },
          { pattern: this.rules.chinese.reg, message: this.rules.chinese.msg, trigger: 'blur' }
        ],
        classname: [
          { message: '请输入班级名称', validator: this.checkValue },
          { pattern: this.rules.chinese.reg, message: this.rules.chinese.msg, trigger: 'blur' }
        ],
        admin_number: [ // 用户账号
          { required: true, validator: this.checkValue, message: '账号由4-16位英文、数字、下划线组成', },
          { pattern: this.rules.account.reg, message: this.rules.account.msg, trigger: 'blur' }
        ],
        admin_password: [ // 用户密码
          { validator: this.checkValue, message: '密码由6-16位英文、数字、下划线组成' },
          { pattern: this.rules.pwd.reg, message: this.rules.pwd.msg, trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getOrgData();
  },
  filters: {
    formatStatus(status) {
      const statusMap = {
        '1': '已启用',
        '2': '已禁用',
        '3': '已毕业',
        '0': '已删除'
      }
      return statusMap[status]
    },
    forCatetype(catetype) {
      const cate_type = {
        '1': '添加学段',
        '2': '添加院系',
        '3': '添加类别'
      }
      return cate_type[catetype]
    },
  },
  methods: {
    handleClose() {
      this.educationForm.eduname = '' // 教育局名称
      this.schoolForm.schname = '' // 学校名称
      this.educationForm.admin_number = '' // 教育局账号
      this.educationForm.admin_password = '' // 教育局密码
      this.schoolForm.admin_number = '' // 学校账号
      this.schoolForm.admin_password = '' // 学校密码
      this.addFacultyForm.facultyname = '' // 院系名称
      this.addFacultyForm.kindname = '' // 类别名称
      this.addFacultyForm.staryname = '' // 学段名称
      this.delEducationForm.admin_password = '' // 管理员密码
      this.addGradeForm.gradename = '' // 年级名称
      this.addGradeForm.majorname = '' // 专业名称
      this.addClassForm.classname = '' // 班级名称
      this.$refs.educationForm && this.$refs.educationForm.clearValidate()
      this.$refs.schoolForm && this.$refs.schoolForm.clearValidate()
      this.$refs.addFacultyForm && this.$refs.addFacultyForm.clearValidate()
      this.$refs.delEducationForm && this.$refs.delEducationForm.clearValidate()
      this.$refs.addClassForm && this.$refs.addClassForm.clearValidate()
    },

    checkValue(rule, value, callback) {
      if (!checkSpace(value)) {
        callback(new Error("请输入有效值"));
      } else {
        callback();
      }
    },
    // 获取组织架构----------------------------------------------------------------------
    getOrgData() {
      this.loading.status = true;
      // 重写成功回调
      let success = res => {
        this.loading.status = false;
        this.orgData = res.data;
        if (this.orgData.length > 0) {
          this.orgData[0].inx = 1;
        }


        this.$forceUpdate(); //强制渲染
      };

      let fail = data => {
        this.loading.status = false;
        this.$message.error(data.msg);
      };
      this.$ajax.get("api/newlevel/levelorg_list", {
        params: {},
        func: { success: success, fail: fail }
      });
    },
    // 获取组织架构----------------------------------------------------------------------
    addEducationTrigger(obj, even) { // 添加/编辑教育局弹窗
      this.addEducationFlag = true
      if (this.$refs.educationForm) {
        this.$refs.educationForm.clearValidate();
      }
      this.edutitle = even
      switch (even) {
        case '添加':
          this.educationForm.server_type = 2
          this.educationForm.pid = obj.id
          break;
        case '编辑':
          this.educationForm.id = obj.id
          this.findEducation(obj)
          break;
        default:
          break;
      }
    },
    findEducation(obj) { // 获取当前教育局/学校信息
      const success = res => {
        if (obj.server_type === 2) {
          this.educationForm.eduname = res.data.name
          this.educationForm.admin_number = res.data.admin_number
        } else {
          this.schoolForm.schname = res.data.name
          this.schoolForm.cate_type = res.data.cate_type
          this.schoolForm.admin_number = res.data.admin_number
        }

      }
      this.$ajax.get('api/newlevel/platform', { params: { id: obj.id, app_code: obj.app_code }, func: { success } })
    },
    addEducationTips(flag) {
      if (this.edutitle === '添加') {
        if (this.educationForm.admin_password == '') {
          this.educationForm.admin_password = '666666'
        }
        if (flag) {
          /*确定的逻辑 */
          this.$refs[`educationForm`].validate(valid => {
            if (valid) {
              this.addEducation();
            } else {
              return;
            }
          })
        } else {
          this.addEducationWinHide();
        }
      } else if (this.edutitle === '编辑') {
        if (flag) {
          if (this.educationForm.admin_password == '') {
            // 部分验证
            this.$refs[`educationForm`].validateField('eduname', valid1 => {
              this.$refs[`educationForm`].validateField('admin_number', valid2 => {
                if (!valid1 && !valid2) {
                  this.editEducation();
                }
              })
            })
          } else {
            /*确定的逻辑 */
            this.$refs[`educationForm`].validate(valid => {
              if (valid) {
                this.editEducation();
              } else {
                return;
              }
            })
          }
        } else {
          this.addEducationWinHide();
        }
      }

    },
    addEducation() { /* 添加教育局 */
      let params = {
        name: this.educationForm.eduname,/*记录添加教育局名称 */
        admin_number: this.educationForm.admin_number,/*记录管理员账号 */
        admin_password: this.educationForm.admin_password,/*记录管理员密码 */
        server_type: this.educationForm.server_type,
        pid: this.educationForm.pid,
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addEducationWinHide()
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.post('api/newlevel/platform', params, { func: { success, fail } })
    },
    editEducation() { /* 编辑教育局 */
      let params
      if (this.educationForm.admin_password) {
        params = {
          name: this.educationForm.eduname,
          id: this.educationForm.id,
          admin_number: this.educationForm.admin_number,
          admin_password: this.educationForm.admin_password
        }
      } else {
        params = {
          name: this.educationForm.eduname,
          id: this.educationForm.id,
          admin_number: this.educationForm.admin_number,
        }
      }

      let success = res => {
        this.$message.success(res.msg);
        this.addEducationWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform', params, { func: { success, fail } })
    },
    addEducationWinHide() { // 教育局 清除输入框内容，关闭弹窗
      this.educationForm.eduname = '';
      this.educationForm.admin_number = '';
      this.educationForm.admin_password = '';
      this.$refs.educationForm.clearValidate();
      this.addEducationFlag = false;
    },

    addSchoolTrigger(obj, even) { // 添加/编辑学校弹窗
      console.log(obj, even)
      this.addSchoolFlag = true
      if (this.$refs.schoolForm) {
        this.$refs.schoolForm.clearValidate()
      }
      this.schtitle = even
      switch (even) {
        case '添加':
          this.schoolForm.server_type = 1
          this.schoolForm.pid = obj.id
          break;
        case '编辑':
          this.schoolForm.id = obj.id
          this.schoolChild = obj.children
          this.findEducation(obj)
          break;
        default:
          break;
      }
    },
    addSchoolTips(flag) {/*控制添加学校的弹窗 */
      if (this.schtitle === '添加') {
        this.schoolForm.admin_password = this.schoolForm.admin_password == '' ? '666666' : this.schoolForm.admin_password
        if (flag) {
          /*确定的逻辑 */
          this.$refs[`schoolForm`].validate(valid => {
            if (valid) {
              this.addSchool();
            } else {
              return;
            }
          })
        } else {
          this.addSchoolWinHide();
        }
      } else if (this.schtitle === '编辑') {
        if (flag) {
          if (this.schoolForm.admin_password == '') {
            this.$refs[`schoolForm`].validateField('schname', valid1 => {
              this.$refs[`schoolForm`].validateField('admin_number', valid2 => {
                if (!valid1 && !valid2) {
                  this.editSchool();
                }
              })
            })
          } else {
            /*确定的逻辑 */
            this.$refs[`schoolForm`].validate(valid => {
              if (valid) {
                this.editSchool();
              } else {
                return;
              }
            })
          }
        } else {
          this.addSchoolWinHide();
        }
      }
    },
    addSchool() { /**添加学校 */
      let params = {
        server_type: this.schoolForm.server_type,
        pid: this.schoolForm.pid,
        name: this.schoolForm.schname,
        cate_type: this.schoolForm.cate_type,
        admin_number: this.schoolForm.admin_number,
        admin_password: this.schoolForm.admin_password,
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addSchoolWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.post('api/newlevel/platform', params, { func: { success, fail } })
    },
    editSchool() {  // 编辑的方法  密码不传时默认不修改
      let params
      if (this.schoolForm.admin_password) {
        params = {
          id: this.schoolForm.id,
          cate_type: this.schoolForm.cate_type,
          name: this.schoolForm.schname,
          admin_number: this.schoolForm.admin_number,
          admin_password: this.schoolForm.admin_password
        }
      } else {
        params = {
          id: this.schoolForm.id,
          cate_type: this.schoolForm.cate_type,
          name: this.schoolForm.schname,
          admin_number: this.schoolForm.admin_number,
        }
      }

      let success = res => {
        this.$message.success(res.msg);
        this.addSchoolWinHide()
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform', params, { func: { success, fail } })
    },
    addSchoolWinHide() { // 清除输入框内容，关闭弹窗
      this.schoolForm.schname = '';
      this.schoolForm.cate_type = 1;
      this.schoolForm.admin_number = '';
      this.schoolForm.admin_password = '';
      this.addSchoolFlag = false;
      this.$refs.schoolForm.clearValidate();
    },

    stopEducationTrigger(obj, even) {
      console.log(obj, even)
      this.delEducationFlag = true
      this.$refs.delEducationForm && this.$refs.delEducationForm.clearValidate()
      this.stopName = obj.name
      this.stopTitle = even
      this.levelFlag = obj.level
      if (even === '删除') {
        if (obj.level === 1) {
          this.stopObj = {
            id: obj.id,
            code: obj.app_code,
          }
        } else {
          this.stopObj = {
            id: obj.id,
          }
        }
      } else if (even === '禁用') {
        if (obj.level === 1) {
          this.stopObj = {
            id: obj.id,
            status: obj.status
          }
        } else {
          this.stopObj = {
            id: obj.id,
            name: obj.name,
            status: obj.status,
            level: obj.level,
            pid: obj.pid,
          }
        }
      } else if (even === '启用') {
        if (obj.level === 1) {
          this.stopObj = {
            id: obj.id,
            status: obj.status
          }
        } else {
          this.stopObj = {
            id: obj.id,
            name: obj.name,
            status: obj.status,
            level: obj.level,
            pid: obj.pid,
          }
        }
      }
    },
    delEducationTips(flag) {/*控制禁用/启用/删除教育局/学校/ 组织架构 的弹窗 */

      let success = res => {
        if (flag) {
          /*确定的逻辑 */
          this.$refs[`delEducationForm`].validate(valid => {
            if (valid) {
              if (this.stopTitle === '删除') {
                if (this.levelFlag === 1) {
                  this.delEducation()
                } else {
                  this.delTissue()
                }
              } else if (this.stopTitle === '禁用') {
                if (this.levelFlag === 1) {
                  this.stopEducation()
                } else {
                  this.stopTissue();
                }
              } else if (this.stopTitle === '启用') {
                if (this.levelFlag === 1) {
                  this.startEducation()
                } else {
                  this.startTissue()
                }
              }
            }
          })
        } else {
          this.delEducationwinHide();
        }
      }
      let fail = res => {
        if (!flag) {
          this.delEducationwinHide();
        } else if (res.status == 'fail') {
          return this.$message.error('密码错误')
        }
      }
      this.$ajax.get('api/user/check_user_by_id', {
        params: {
          id: this.$g.user_id,
          password: this.delEducationForm.admin_password
        }, func: { success, fail }
      })
    },
    delEducation() {/*删除教育局/学校 */
      let params = {
        id: this.stopObj.id,
        app_code: this.stopObj.app_code
      }
      const success = res => {
        this.$message.success(res.msg);
        this.delEducationwinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.delete('api/newlevel/platform', { params, func: { success, fail } })
    },
    delTissue() { /*删除组织 */
      let params = {
        id: this.stopObj.id,
      }
      const success = res => {
        this.$message.success(res.msg);
        this.delEducationwinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.delete('api/newlevel/platform_org', { params, func: { success, fail } })
    },
    stopEducation() { /*禁用教育局/学校 */
      let params = {
        id: this.stopObj.id,
        status: this.stopObj.status + 1,
      }
      const success = res => {
        this.$message.success(res.msg);
        this.delEducationwinHide()
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform', params, { func: { success, fail } })
    },
    stopTissue() { /*禁用组织 */
      let params = {
        id: this.stopObj.id,
        name: this.stopObj.name,
        status: this.stopObj.status + 1,
        level: this.stopObj.level,
        pid: this.stopObj.pid
      }
      const success = res => {
        this.$message.success(res.msg);
        this.delEducationwinHide()
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform_org', params, { func: { success: success, fail: fail } })
    },
    startEducation() { /*启用教育局/学校 */
      let params = {
        id: this.stopObj.id,
        status: this.stopObj.status - 1,
      }
      const success = res => {
        this.$message.success(res.msg);
        this.delEducationwinHide()
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform', params, { func: { success, fail } })
    },
    startTissue() { /*启用组织 */
      let params = {
        id: this.stopObj.id,
        name: this.stopObj.name,
        status: this.stopObj.status - 1,
        level: this.stopObj.level,
        pid: this.stopObj.pid
      }
      const success = res => {
        this.$message.success(res.msg);
        this.delEducationwinHide()
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform_org', params, { func: { success, fail } })
    },
    delEducationwinHide() {
      this.delEducationForm.admin_password = ''
      this.delEducationFlag = false;
      this.$refs.delEducationForm && this.$refs.delEducationForm.clearValidate()
    },

    addFacultyTrigger(obj, even) {
      console.log(obj, even)
      this.cat_Faculty = even
      this.addFacultyFlag = true
      this.$refs.addFacultyForm && this.$refs.addFacultyForm.clearValidate()
      if (even === '添加') {
        this.addFacultyForm.pid = 1
        this.addFacultyForm.level = 2
        this.addFacultyForm.status = obj.status
        this.addFacultyForm.app_code = obj.app_code
        if (obj.cate_type === 1) {
          this.FacultyTitle = '添加学段'
          this.addFacultyForm.cate_type = obj.cate_type
        } else if (obj.cate_type === 2) {
          this.FacultyTitle = '添加院系'
          this.addFacultyForm.cate_type = obj.cate_type
        } else if (obj.cate_type === 3) {
          this.FacultyTitle = '添加类别'
          this.addFacultyForm.cate_type = obj.cate_type
        }
      } else if (even === '编辑') {
        this.addFacultyForm.id = obj.id
        this.addFacultyForm.pid = obj.pid
        this.addFacultyForm.level = obj.level
        this.addFacultyForm.status = obj.status
        if (obj.cate_type === 1) {
          this.FacultyTitle = '编辑学段'
          this.addFacultyForm.cate_type = obj.cate_type
          this.addFacultyForm.staryname = obj.name
        } else if (obj.cate_type === 2) {
          this.FacultyTitle = '编辑院系'
          this.addFacultyForm.cate_type = obj.cate_type
          this.addFacultyForm.facultyname = obj.name
        } else if (obj.cate_type === 3) {
          this.FacultyTitle = '编辑类别'
          this.addFacultyForm.cate_type = obj.cate_type
          this.addFacultyForm.kindname = obj.name
        }
      }
    },
    addFacultyTips(flag) { /**控制添加学段/院系/类别 弹窗 */
      if (flag) {
        this.$refs[`addFacultyForm`].validate(valid => {
          if (valid) {
            if (this.cat_Faculty === '添加') {
              this.addFaculty()
            } else if (this.cat_Faculty === '编辑') {
              this.editFaculty()
            }
          } else {
            return
          }
        })
      } else {
        this.addFacultyWinHide()
      }
    },
    addFaculty() { /**添加学段/院系/类别 */
      let params
      if (this.addFacultyForm.cate_type === 1) { // 学段
        params = {
          name: this.addFacultyForm.staryname,
          pid: this.addFacultyForm.pid,
          level: this.addFacultyForm.level,
          status: this.addFacultyForm.status,
          app_code: this.addFacultyForm.app_code
        }
      } else if (this.addFacultyForm.cate_type === 2) { // 院系
        params = {
          name: this.addFacultyForm.facultyname,
          pid: this.addFacultyForm.pid,
          level: this.addFacultyForm.level,
          status: this.addFacultyForm.status,
          app_code: this.addFacultyForm.app_code
        }
      } else { // 类别
        params = {
          name: this.addFacultyForm.kindname,
          pid: this.addFacultyForm.pid,
          level: this.addFacultyForm.level,
          status: this.addFacultyForm.status,
          app_code: this.addFacultyForm.app_code
        }
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addFacultyWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.post('api/newlevel/platform_org', params, { func: { success, fail } })
    },
    editFaculty() { /** 编辑学段/院系/类别*/
      let params
      if (this.addFacultyForm.cate_type === 1) { // 学段
        params = {
          id: this.addFacultyForm.id,
          name: this.addFacultyForm.staryname,
          pid: this.addFacultyForm.pid,
          level: this.addFacultyForm.level,
          status: this.addFacultyForm.status,
        }
      } else if (this.addFacultyForm.cate_type === 2) { // 院系
        params = {
          id: this.addFacultyForm.id,
          name: this.addFacultyForm.facultyname,
          pid: this.addFacultyForm.pid,
          level: this.addFacultyForm.level,
          status: this.addFacultyForm.status,
        }
      } else { // 类别
        params = {
          id: this.addFacultyForm.id,
          name: this.addFacultyForm.kindname,
          pid: this.addFacultyForm.pid,
          level: this.addFacultyForm.level,
          status: this.addFacultyForm.status,
        }
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addFacultyWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform_org', params, { func: { success, fail } })
    },
    addFacultyWinHide() { /**清除输入框内容，关闭弹窗 */
      this.addFacultyForm.staryname = ''
      this.addFacultyForm.facultyname = ''
      this.addFacultyForm.kindname = ''
      this.$refs.addFacultyForm && this.$refs.addFacultyForm.clearValidate()
      this.addFacultyFlag = false
    },

    addGradeFlagTrigger(obj, even) {
      console.log(obj, even)
      this.addGradeFlag = true
      this.$refs.addGradeForm && this.$refs.addGradeForm.clearValidate()
      this.addGradeForm.cate_type = obj.cate_type
      this.gradeEven = even
      if (even === '添加') {
        this.addGradeForm.pid = obj.id
        this.addGradeForm.level = 3
        this.addGradeForm.status = obj.status
        this.addGradeForm.app_code = obj.app_code
        if (obj.cate_type === 1) {
          this.gradeTitle = '添加年级'
        } else {
          this.gradeTitle = '添加专业'
        }
      } else {
        if (obj.cate_type === 1) {
          this.gradeTitle = '编辑年级'
          this.addGradeForm.gradename = obj.name
        } else {
          this.gradeTitle = '编辑专业'
          this.addGradeForm.majorname = obj.name
        }
        this.addGradeForm.id = obj.id
        this.addGradeForm.pid = obj.pid
        this.addGradeForm.level = obj.level
        this.addGradeForm.status = obj.status
      }
    },
    addGradeTips(flag) { /**控制添加/ 编辑 年级/专业 弹窗 */
      if (flag) {
        this.$refs[`addGradeForm`].validate(valid => {
          if (valid) {
            if (this.gradeEven === '添加') {
              this.addGrade()
            } else {
              this.editGrade()
            }
          } else {
            return
          }
        })
      } else {
        this.addGradeWinHide();
      }
    },
    addGrade() { /**添加年级/ 专业 */
      let params
      if (this.addGradeForm.cate_type === 1) {
        params = {
          name: this.addGradeForm.gradename,
          pid: this.addGradeForm.pid,
          level: this.addGradeForm.level,
          status: this.addGradeForm.status,
          app_code: this.addGradeForm.app_code
        }
      } else {
        params = {
          name: this.addGradeForm.majorname,
          pid: this.addGradeForm.pid,
          level: this.addGradeForm.level,
          status: this.addGradeForm.status,
          app_code: this.addGradeForm.app_code
        }
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addGradeWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.post('api/newlevel/platform_org', params, { func: { success, fail } })
    },
    editGrade() { /**编辑年级/ 专业 */
      let params
      if (this.addGradeForm.cate_type === 1) {
        params = {
          id: this.addGradeForm.id,
          name: this.addGradeForm.gradename,
          pid: this.addGradeForm.pid,
          level: this.addGradeForm.level,
          status: this.addGradeForm.status,
        }
      } else {
        params = {
          id: this.addGradeForm.id,
          name: this.addGradeForm.majorname,
          pid: this.addGradeForm.pid,
          level: this.addGradeForm.level,
          status: this.addGradeForm.status,
        }
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addGradeWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform_org', params, { func: { success, fail } })
    },
    addGradeWinHide() {  /**清除输入框内容，关闭弹窗 */
      this.addGradeForm.gradename = ''
      this.addGradeForm.majorname = ''
      this.$refs.addGradeForm && this.$refs.addGradeForm.clearValidate()
      this.addGradeFlag = false
    },

    addClassFlagTrigger(obj, even) {
      // this.getAddClassList(obj)
      console.log(obj, even)
      this.addClassFlag = true
      this.$refs.addClassForm && this.$refs.addClassForm.clearValidate()
      this.classTitle = even
      if (even === '添加') {
        this.addClassForm.pid = obj.id
        this.addClassForm.level = 4
        this.addClassForm.status = obj.status
        this.addClassForm.app_code = obj.app_code
      } else {
        this.addClassForm.id = obj.id
        this.addClassForm.pid = obj.pid
        this.addClassForm.classname = obj.name
        this.addClassForm.level = obj.level
        this.addClassForm.status = obj.status
      }
    },
    addClassTips(flag) { /**控制添加/ 编辑 班级 弹窗 */
      if (flag) {
        this.$refs[`addClassForm`].validate(valid => {
          if (valid) {
            if (this.classTitle === '添加') {
              this.addClass()
            } else {
              this.editClass()
            }
          } else {
            return
          }
        })
      } else {
        this.addClassWinHide();
      }
    },
    addClass() {  // 添加班级的方法
      let params = {
        name: this.addClassForm.classname,
        pid: this.addClassForm.pid,
        level: this.addClassForm.level,
        status: this.addClassForm.status,
        app_code: this.addClassForm.app_code
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addClassWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.post('api/newlevel/platform_org', params, { func: { success, fail } })
    },
    editClass() { // 编辑班级的方法
      let params = {
        id: this.addClassForm.id,
        name: this.addClassForm.classname,
        pid: this.addClassForm.pid,
        level: this.addClassForm.level,
        status: this.addClassForm.status,
      }
      const success = res => {
        this.$message.success(res.msg);
        this.addClassWinHide();
        this.getOrgData();
      }
      const fail = res => {
        this.$message.error(res.msg);
      }
      this.$ajax.put('api/newlevel/platform_org', params, { func: { success, fail } })
    },
    addClassWinHide() { /**清除输入框内容，关闭弹窗 */
      this.addClassForm.classname = ''
      this.$refs.addClassForm && this.$refs.addClassForm.clearValidate()
      this.addClassFlag = false
    },

    // 获取可添加的班级
    // getAddClassList(obj) {
    //     const success = (res) => {
    //         this.addClassList = res.data;
    //     };
    //     let params = {
    //         id: obj.id,
    //     };
    //     this.$ajax.get("api/org/get_name_by_school_grade", {
    //         params: params,
    //         func: { success: success },
    //     });
    // },
  }
};
</script>

<style lang="scss">
.zh-organization {
  .frame-manage {
    .el-tree {
      height: auto;
    }

    .frame-list {
      display: flex;
      height: 40px;
      line-height: 40px;
      background: #ddd;
      color: #000;
      text-align: center;

      li {
        flex: 1;
        box-sizing: border-box;

        &:nth-of-type(1) {
          flex: 0 0 60px;
        }

        &:nth-of-type(3) {
          flex: 0 0 160px;

          .link {
            color: #348EFC;
            cursor: pointer;
          }
        }

        &:nth-of-type(4) {
          flex: 0 0 160px;
        }
      }
    }

    .custom-tree-node {
      width: 100%;

      .frame-list {
        background: none;
        border-bottom: 1px solid #ddd;

        li {
          position: relative;
          border-left: 1px solid #ddd;

          &:last-child {
            border-right: 1px solid #ddd;
          }
        }

        img {
          display: inline-block;
          vertical-align: middle;
        }
      }
    }

    .el-tree-node__content {
      padding-left: 0 !important;
      height: auto;
    }

    .el-tree-node__content {
      li:nth-of-type(2) .ellips {
        text-align: left;
        padding-left: 5%;

        .gap {
          display: inline-block;
          width: 18px;
        }
      }
    }
  }
}
</style>
